//
//  SystemViewController.m
//  mhomeapp
//
//  Created by 刘春雷 on 16/4/15.
//  Copyright © 2016年 刘春雷. All rights reserved.
//

#import "SystemViewController.h"

@interface SystemViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,copy) NSArray *array;
@end

@implementation SystemViewController

- (void)viewDidLoad {
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0];//背景颜色
    [super viewDidLoad];
    self.title =@"设置";
    UITableView *table = [[UITableView alloc]initWithFrame:CGRects(0, 20, 375, 130) style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    table.scrollEnabled = NO;
    table.rowHeight=50*BIZ;//设置行高
    table.separatorStyle = NO;//去掉线
    [self.view addSubview:table];
    self.array = @[@"清理缓存",@"版本:"];
}

//返回行数
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;

}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *inde = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:inde];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:inde];
    }
      cell.textLabel.text = self.array[indexPath.row];
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
 
    if (indexPath.row == 1) {
        cell.detailTextLabel.text =  [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
        }else{
       cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator ;
    }

   
    return cell;
}
//cell被选中事件
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0)
    {
        UIAlertView * alert=[[UIAlertView alloc]initWithTitle:nil message:@"您确定要清除缓存吗？" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        [alert show];
    
    }

}
//协议中的方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"%ld",(long)buttonIndex);//buttonIndex对应的是 确定和取消＝＝0 时  是确定
    if (buttonIndex==0) {
        //===============清除缓存==============
        NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachePath];
        NSLog(@"文件数 ：%lu",(unsigned long)[files count]);
        if ([files count] ==0) {
            //提醒框
            [QMUITips showInfo:@"已经清理过了" inView:self.view hideAfterDelay:1];
        }else{
            for (NSString *p in files)
            {
                NSError *error;
                NSString *path = [cachePath stringByAppendingString:[NSString stringWithFormat:@"/%@",p]];
                if([[NSFileManager defaultManager] fileExistsAtPath:path])
                {
                    [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                     [QMUITips showInfo:@"清理成功" inView:self.view hideAfterDelay:1];
                }
            }
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
